// pages/limitTimeBy/limitTimeBy.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperWidth: 0,
    sum:0,
    time: [
      { id: 0, font: '正在抢', time: '10:00' },
      { id: 1, font: '未开始', time: '20:00' },
      { id: 2, font: '已结束', time: '10:00' },
      // { id: 3, font: '2019-4-20', time: '20:00' },
    ],
    showSwiper: true,
    times:{
      hour:10,
      minute:20,
      second:20
    },
    product: [],
  
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.setData({
      imgPath: app.d.imgPath
    })
    var sucFunc = function (that, res) {
      that.setData({
        product: res.data.res,
        starttime:res.data.time
      })
      that.countdowns(res.data.time)
      wx.hideLoading();
    }
    var data = { openid: app.d.openid }
    utils.wxRequest(that, app.d.path, "Spike/index", data, sucFunc);
  },
  swiper:function(e){
    var that = this;
    var time = e.currentTarget.dataset.id + 1;
    wx.showLoading({
      title: '加载中',
    })
    var sucFunc = function (that, res) {
      that.setData({
        product: res.data.res,
        starttime: res.data.time
      })
      that.countdowns(res.data.time)
      wx.hideLoading();
    }
    var data = { openid: app.d.openid,time:time }
    utils.wxRequest(that, app.d.path, "Spike/index", data, sucFunc);
  },
  countdowns:function(time){
    var that = this;
    if (time < 0) {
      that.setData({
        'times.hour': '00',
        'times.minute': '00',
        'times.second': '00'
      })
      return
    }
    that.setData({
      'times.hour': Math.floor(time / 3600 % 24),
      'times.minute': Math.floor(time / 60 % 60),
      'times.second': Math.floor(time % 60)
    })
    setTimeout(function () {
      that.setData({
        starttime: that.data.starttime - 1
      });
      that.countdowns(that.data.starttime);
    }, 1000)
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (this.data.time.length >= 4) {
      this.setData({
        num: 4,
        showSwiper: true
      })
    } else {
      this.setData({
        showSwiper: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})